<?php
class Library_Model_TinTucMapper extends Library_Model_DataMapperAbstract
{
	protected static $_instance = null;	
	private $_paginator = null;

	public function searchTinTucById($mabaiviet)
	{
		$db = $this->getDb();
		$selectBaiviet = $db->select()
						   ->from('tbl_tintuc')
						   ->where('MaBaiViet = ?',$mabaiviet);
		$baiviet = $db->fetchRow($selectBaiviet);
		
		$chudeMapper = Library_Model_ChuDeMapper::getInstance();
		$chude = $chudeMapper->searchChuDeById($baiviet['MaChuDe']);
		$baiviet['tbl_chude'] = $chude;
			
		$nguoidungMapper =Library_Model_NguoiDungMapper::getInstance();
		$nguoidung = $nguoidungMapper->searchById($baiviet['MaND']);
		$baiviet['tbl_nguoidung'] = $nguoidung;
		
		return $this->_populate($baiviet);
	}
	
	public function searchByMachude($id)
	{
		$db = $this->getDb();
		$selectBaiviet = $db->select()
							->from('tbl_tintuc')
							->where('MaChuDe = ?',$id)
							->limit(3,0);
		$baiviets = $db->fetchAll($selectBaiviet);
	
		$baivietArray = array();
		
		foreach($baiviets as $baiviet)
		{
		$nguoidungMapper =Library_Model_NguoiDungMapper::getInstance();
		$nguoidung = $nguoidungMapper->searchById($baiviet['MaND']);
		$baiviet['tbl_nguoidung'] = $nguoidung;
	
		$baivietArray[] = $this->_populate($baiviet);
		}
		return $baivietArray;
	}
	
	public function baivietList()
	{
		$db = $this->getDb();
		$selectBaiviet = $db->select()
						   ->from('tbl_tintuc');
		$baiviets = $db->fetchAll($selectBaiviet);
		$baivietObjArray = array();
		foreach($baiviets as $baiviet)
		{
			$chudeMapper = Library_Model_ChuDeMapper::getInstance();
			$chude = $chudeMapper->searchChuDeById($baiviet['MaChuDe']);
			$baiviet['tbl_chude'] = $chude;
			
			$nguoidungMapper =Library_Model_NguoiDungMapper::getInstance();
			$nguoidung = $nguoidungMapper->searchById($baiviet['MaND']);
			$baiviet['tbl_nguoidung'] = $nguoidung;
			
			$baivietObjArray[] = $this->_populate($baiviet);
			
		}
		return $baivietObjArray;
		
	}
	// hien thi
	public function _populate($data)
	{
		$obj = new Library_Model_TinTuc();
		$obj->setId($data['MaBaiViet']);
		$obj->setMachude($data['MaChuDe']);
		$obj->setMand($data['MaND']);
		$obj->setTenbaiviet($data['TenBaiViet']);
		$obj->setNoidung($data['NoiDung']);
		$obj->setNgaydang($data['NgayDang']);
		$obj->setLuotxem($data['LuotXem']);
		$obj->setKiemduyet($data['KiemDuyet']);
		$obj->setTomtat($data['TomTat']);
		$obj->setNguoidung($data['tbl_nguoidung']);
		$obj->setTenchude($data['tbl_chude']);
		
		return $obj;
	}
	protected function _insert(Library_Model_DomainObjectAbstract $obj){
	
		$db = $this->getDb();
		$data = array(
				'MaChuDe' 	=> $obj->getMachude(),
				'MaND'		=>$obj->getMand(),
				'TenBaiViet'	=>$obj->getTenbaiviet(),
				'NoiDung'	=>$obj->getNoidung(),
				'NgayDang'	=>$obj->getNgaydang(),
				'LuotXem'	=>$obj->getLuotxem(),
				'KiemDuyet'	=>$obj->getKiemduyet(),
				'TomTat'		=>$obj->getTomtat()
				);
		$db->insert('tbl_tintuc',$data);
	}
	protected function _update(Library_Model_DomainObjectAbstract $obj){
		
		$db = $this->getDb();
		$data = array(
				'MaChuDe' 	=> $obj->getMachude(),
				'MaND'		=>$obj->getMand(),
				'TenBaiViet'	=>$obj->getTenbaiviet(),
				'NoiDung'	=>$obj->getNoidung(),
				'NgayDang'	=>$obj->getNgaydang(),
				'LuotXem'	=>$obj->getLuotxem(),
				'KiemDuyet'	=>$obj->getKiemduyet(),
				'TomTat'		=>$obj->getTomtat()
		);
		$where = $db->quoteInto('MaBaiViet = ?',$obj->getId());
		$db->update('tbl_tintuc',$data,$where);
	}
	
	public function delete($id)
	{
		$db = $this->getDb();
		$where = $db->quoteInto('MaBaiViet = ?',$id);
		$db->delete('tbl_tintuc',$where);
	}
	
	public static function getInstance()
	{
		if(null === self::$_instance)
		{
			self::$_instance = new self();				
		}
		return self::$_instance;
	}
}